package com.lqk.client;

import java.io.IOException;
import java.net.InetSocketAddress;
import java.nio.channels.SelectionKey;
import java.nio.channels.Selector;
import java.nio.channels.SocketChannel;
import java.nio.charset.Charset;
import java.util.Scanner;

/**
 * 聊天室-客户端
 */
public class ChatClient {

    // 启动客户端的方法
    public void startClient(String name) throws Exception {
        // 1 连接服务端
        SocketChannel socketChannel = SocketChannel.open(new InetSocketAddress("127.0.0.1",8000));

        // 接收服务器端响应的数据
        Selector selector = Selector.open();
        socketChannel.configureBlocking(false);
        socketChannel.register(selector, SelectionKey.OP_READ);
        // 创建线程
        new Thread(new ClientThread(selector)).start();

        // 2 向服务端发送消息
        Scanner scanner=new Scanner(System.in);
        while (scanner.hasNext()){
            String msg = scanner.nextLine();
            if (msg.length()>0){
                socketChannel.write(Charset.forName("UTF-8").encode(name+" : "+msg));
            }
        }
    }

}
